package MULE.Model.Panel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JPanel;

import MULE.Model.Game;
import MULE.Model.Player;
import MULE.Model.Logic.MuleDriver;

public class FooterPanel extends JPanel {

	public static final int PANEL_HEIGHT = 100;

	private PlayerScorePanel[] scorePanels;
	private MenuButtonPanel menuPanel;

	/**
	 * Creates a new scoreboard panel and shows the score of given players.
	 * 
	 * @param players
	 *            The game's players.
	 */
	public FooterPanel(ArrayList<Player> players) {
		// Initialize panel.
		Dimension dim = new Dimension(MuleDriver.WINDOW_WIDTH, PANEL_HEIGHT);
		this.setMinimumSize(dim);
		this.setPreferredSize(dim);
		this.setMaximumSize(dim);
		this.setBackground(Color.WHITE);
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		((FlowLayout) this.getLayout()).setVgap(0);
		((FlowLayout) this.getLayout()).setHgap(2);

		menuPanel = new MenuButtonPanel();
		add(menuPanel);
		// Instantiate and add player labels.
		scorePanels = new PlayerScorePanel[Game.MAX_NUM_PLAYERS];
		for (int i = 0; i < scorePanels.length; ++i) {
			scorePanels[i] = new PlayerScorePanel();
			add(scorePanels[i]);
		}
	}

	/**
	 * Updates the given player's score.
	 * 
	 * @param player
	 *            The player whose score should be updated.
	 */
	public void updateScore(Player player) {
		// Get panel index (panels are in player num order).
		int playerNum = player.getId() - 1;
		scorePanels[playerNum].update(player);
	}

	/**
	 * Sets the given action listener to listen to the menu button.
	 * 
	 * @param listener
	 *            The menu button's action listener.
	 */
	public void addMenuButtonListener(ActionListener listener) {
		this.menuPanel.addMenuButtonListener(listener);
	}
}
